
//
//  RunoneMainViewController.m
//  Runone
//
//  Created by runone01 on 16/7/28.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "RunoneMainViewController.h"
#import "PreviewViewController.h"
#import "GuestCarViewController.h"
#import "ManagementCarViewController.h"
#import "UIColor+Addition.h"
#import "RightCodeTool.h"
#import "EventManagementViewController.h"
#import "OneConst.h"
#import "RequestHelper.h"
#import "RequestGinsengModel.h"
#import "AppLocalManager.h"
#import "Define.h"
#import "AppDelegate.h"
#import "MJExtension.h"
#import "notiModel.h"

@interface RunoneMainViewController ()<UITabBarDelegate>
{
    UIView*notiView;
}
@property (nonatomic, strong) PreviewViewController         *preVc;
@property (nonatomic, strong) GuestCarViewController        *gusetVc;
@property (nonatomic, strong) ManagementCarViewController   *managerVc;
@property (nonatomic, strong) EventManagementViewController *eventVc;


@property (nonatomic, assign) BOOL isReady;

@property (nonatomic, strong) UITabBarItem *selectItem;
@end

@implementation RunoneMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubViewController];
    _isReady = YES;
    
    AppDelegate*delegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
    if (!delegate.socket) {
        delegate.socket=[[SRWebSocket alloc]initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:GetWSURL]]];
    }
    
    delegate.socket.delegate=self;
    self.socket=delegate.socket;
   
     [self.socket open];
    
    
}

#pragma mark - SRWebSocketDelegate 写上具体聊天逻辑
- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
{
    NSString*token=[[NSUserDefaults standardUserDefaults]objectForKey:ACCESS_TOKEN];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self.socket sendString:[NSString stringWithFormat:@"SystemCode=GuangleHighway&Token=%@",token] error:nil];
    
}
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error
{
    NSLog(@"error===%@",error);
}
//监控消息
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
{
     NSLog(@"message======%@",message);
    RequestOutginseng *requestmodel=[RequestOutginseng mj_objectWithKeyValues:message];
     NSString*string=requestmodel.DataValue;
    if ([string isEqualToString:@""]) {
        return;
    }
    NSString*str=[string substringToIndex:1];
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    if ([str isEqualToString:@"1"]||[str isEqualToString:@"2"]) {
        
        [dic setObject:string forKey:@"两客一危"];
        [dic copy];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"liangkeyiwei" object:self userInfo:dic];
        
    }
    else if ([str isEqualToString:@"3"])
    {
        [dic setObject:string forKey:@"车辆管理"];
        [dic copy];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"carmanager" object:self userInfo:dic];
        
    }
    else if ([str isEqualToString:@"4"]||[str isEqualToString:@"5"])
    {
       
        [self createLocalNotificationWith:string];
    }   
}

//通知
-(void)createLocalNotificationWith:(NSString*)string {
    
    
    NSString*jsonStr=[string substringFromIndex:2];
    NSMutableArray*mulArray=[notiModel mj_objectArrayWithKeyValuesArray:jsonStr];
    notiModel*model;
    if (mulArray.count!=0) {
        model=mulArray[0];
    }
    if (!model.Speed) {
        model.Speed=@"0";
    }
    
       
    
    
    UIWindow*window=[UIApplication sharedApplication].windows[0];
    notiView=[window viewWithTag:12345];
    UIImageView*logoImageView=[notiView viewWithTag:111];
    UILabel*titleLable=[notiView viewWithTag:222];
    UILabel*detailLable=[notiView viewWithTag:333];
    
    
    if (!notiView) {
        notiView=[[UIView alloc]initWithFrame:CGRectMake(10, -80, self.view.bounds.size.width-20, 80)];
        notiView.backgroundColor=[UIColor lightGrayColor];
        notiView.clipsToBounds=YES;
        notiView.tag=12345;
        notiView.layer.cornerRadius=5;
        
        logoImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 50, 50)];
        logoImageView.backgroundColor=[UIColor clearColor];
        logoImageView.clipsToBounds=YES;
        logoImageView.layer.cornerRadius=5;
        logoImageView.tag=111;
        logoImageView.image=[UIImage imageNamed:@"guanglelogo"];
        [notiView addSubview:logoImageView];
        
        titleLable=[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 180, 30)];
        titleLable.backgroundColor=[UIColor clearColor];
        titleLable.text=model.AlarmName;
        titleLable.textColor=[UIColor blackColor];
        titleLable.font=[UIFont systemFontOfSize:17];
        titleLable.tag=222;
        [notiView addSubview:titleLable];
        
        UILabel*timeLable=[[UILabel alloc]initWithFrame:CGRectMake(notiView.bounds.size.width-160, 10, 80, 30)];
        timeLable.backgroundColor=[UIColor clearColor];
        timeLable.text=@"现在";
        timeLable.textAlignment=2;
        timeLable.textColor=[UIColor blackColor];
        timeLable.font=[UIFont systemFontOfSize:17];
        [notiView addSubview:timeLable];
        
        detailLable=[[UILabel alloc]initWithFrame:CGRectMake(70, 40, notiView.bounds.size.width-80, 30)];
        detailLable.backgroundColor=[UIColor clearColor];
        
        detailLable.text=[NSString stringWithFormat:@"车牌号：%@ 速度：%@ ",model.VehicleNo,model.Speed];
        detailLable.tag=333;
        detailLable.textColor=[UIColor blackColor];
        detailLable.font=[UIFont systemFontOfSize:14];
        [notiView addSubview:detailLable];
        
 
    }
    
    
   
    [window addSubview:notiView];
    
   [UIView animateWithDuration:0.5 animations:^{
       notiView.frame=CGRectMake(10, 10, self.view.bounds.size.width-20, 80);
   } completion:^(BOOL finished) {
       
       [self performSelector:@selector(delayMethod) withObject:nil afterDelay:2.0];
   }];
    
    
}

-(void)delayMethod
{
    [notiView removeFromSuperview];
}

//加载数据
- (void)requestSysRightGroupCode {
    RequestHelper *helper = [[RequestHelper alloc] init];
    
    RequestGinsengModel *GetSysRightGroup = [[RequestGinsengModel alloc] init];
    GetSysRightGroup.URL         = GetSysRightGroupCode;
    GetSysRightGroup.DataValue   = @"";
    GetSysRightGroup.UpdatedTime = @"";
    GetSysRightGroup.SystemCode  = @"GuangleHighway";
    GetSysRightGroup.MethodName  = @"GetSysRightGroupCode";
    
    helper.block = ^(RequestOutginseng *requestOutginseng, id dataValueModel) {
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            
            [self addSubViewController];
        }
        else {
            return;
        }
        
        [[AppLocalManager sharedManager] saveData:requestOutginseng.DataValue  andKey:SYSRIGHTGROUPCODE];
    };
    [helper RequestGinsengModel:GetSysRightGroup andPushController:self];
}
//	@"P01,P02,P0201,P020401,P03,P0301,P030101,P030102,P030103,P0302,P0303,P0304,P030401,P030402,P0306,P04,P04010101,P04010103,P04010201,P04010203,P04010204,P040201,P040202,P040405,P05,"

//添加主界面子控制器
-(void)addSubViewController {
    //高速全览
    PreviewViewController *previewVC      = [[PreviewViewController alloc] init];
    UINavigationController *previewNav    = [[UINavigationController alloc] initWithRootViewController:previewVC];
    previewNav.navigationBar.barTintColor = [UIColor colorWithHexString:@"38b1dc"];
    previewNav.navigationBar.tintColor    = [UIColor whiteColor];
    [previewVC.tabBarItem setImage:[UIImage imageNamed:@"quanlanhui"]];
    [previewVC.tabBarItem setSelectedImage:[UIImage imageNamed:@"gaosuquanlan"]];//
    previewNav.tabBarItem.title = @"高速全览";
    _preVc = previewVC;
    
    //两客一危
    GuestCarViewController *guestCarVC  = [[GuestCarViewController alloc] init];
    UINavigationController *guestCarNav = [[UINavigationController alloc] initWithRootViewController:guestCarVC];
    [guestCarVC.tabBarItem setImage:[UIImage imageNamed:@"liangkeyiwei"]];
    [guestCarVC.tabBarItem setSelectedImage:[UIImage imageNamed:@"liangkelan"]];
    guestCarNav.navigationBar.barTintColor = [UIColor colorWithHexString:@"#38b1dc"];
    guestCarNav.tabBarItem.title = @"两客一危";
    _gusetVc = guestCarVC;
    
    //车辆管理
    ManagementCarViewController *managementCarVC = [[ManagementCarViewController alloc] init];
    UINavigationController *managementCarNav     = [[UINavigationController alloc] initWithRootViewController:managementCarVC];
    [managementCarVC.tabBarItem setImage:[UIImage imageNamed:@"cheliang"]];
    [managementCarVC.tabBarItem setSelectedImage:[UIImage imageNamed:@"cheliangguanli"]];
    managementCarNav.tabBarItem.title           = @"车辆管理";
    _managerVc                                  = managementCarVC;
    managementCarNav.navigationBar.barTintColor = [UIColor colorWithHexString:@"#38b1dc"];
    managementCarNav.navigationBar.tintColor    = [UIColor whiteColor];
    
    //事件管理
    EventManagementViewController *managementIncidentVC = [[EventManagementViewController alloc] init];
    UINavigationController *managementIncidentnav=[[UINavigationController alloc] initWithRootViewController:managementIncidentVC];
    [managementIncidentVC.tabBarItem setImage:[UIImage imageNamed:@"shijian1"]];
    [managementIncidentVC.tabBarItem setSelectedImage:[UIImage imageNamed:@"shijian"]];
    managementIncidentnav.tabBarItem.title           = @"事件管理";
    managementIncidentnav.navigationBar.barTintColor = [UIColor colorWithHexString:@"#38b1dc"];
    managementIncidentnav.navigationBar.tintColor    = [UIColor whiteColor];
    _eventVc = managementIncidentVC;
    
    
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    // ******** 权限 *********
    if ([[RightCodeTool  sharedTool]rightIsEqualTO:@"P02"] && [[RightCodeTool sharedTool]rightIsEqualTO:@"P02"]) { // 两客一危 // 车辆管理
        self.viewControllers=@[previewNav,guestCarNav,managementCarNav,managementIncidentnav];
    }
    if (![[RightCodeTool sharedTool]rightIsEqualTO:@"P02"] && [[RightCodeTool sharedTool]rightIsEqualTO:@"P03"]) {
        self.viewControllers=@[previewNav,managementCarNav,managementIncidentnav];
    }
    if ([[RightCodeTool sharedTool]rightIsEqualTO:@"P02"] && ![[RightCodeTool sharedTool]rightIsEqualTO:@"P03"]) {
        self.viewControllers=@[previewNav, guestCarNav, managementIncidentnav];
    }
    if (![[RightCodeTool sharedTool]rightIsEqualTO:@"P02"] && ![[RightCodeTool sharedTool]rightIsEqualTO:@"P03"]) {
        self.viewControllers=@[previewNav, managementIncidentnav];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    if (self.selectItem == item ) {
        [[NSNotificationCenter defaultCenter] postNotificationName:ROTabBarButtonRepeatClickNotification object:nil];
    }
    self.selectItem = item;
}

//移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
